package com.vnp.cooking.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.vnp.cooking.ChitietCongthucActivity;
import com.vnp.cooking.R;
import com.vnp.cooking.XemInfoUserActivity;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.utils.Utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NhabepGridAvapter extends ArrayAdapter<ObjCookpad> {

	Context context;
	ArrayList<ObjCookpad> objCookpads;
	// khoi tao image loader
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory(false).cacheOnDisc(true)
			.bitmapConfig(Config.RGB_565).build();

	public NhabepGridAvapter(Context context, ArrayList<ObjCookpad> objCookpads) {
		super(context, R.layout.nhabep_grid_item, objCookpads);
		this.context = context;
		this.objCookpads = objCookpads;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		ViewHolder holder = null;
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.nhabep_grid_item, parent, false);

			holder = new ViewHolder();

			holder.imgMonan = (ImageView) v.findViewById(R.id.img_monan);
			holder.txtTenmonan = (TextView) v.findViewById(R.id.txt_ten_mon_an);
			holder.txtViewer = (TextView) v.findViewById(R.id.txt_viewer);
			holder.txtLiker = (TextView) v.findViewById(R.id.txt_liked_count);
			
			//font
			holder.txtTenmonan.setTypeface(Utils.getTypefaceBold(context), Typeface.BOLD);
			holder.txtViewer.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtLiker.setTypeface(Utils.getTypefaceRegular(context));
			

			v.setTag(holder);

		} else {
			holder = (ViewHolder) v.getTag();
		}

		ImageLoader.getInstance().displayImage(
				objCookpads.get(position).getUrlImageCookpad(),
				holder.imgMonan, options);
		if (objCookpads.get(position).getNameCookpad().length() > 15) {
			holder.txtTenmonan.setText(objCookpads.get(position)
					.getNameCookpad().substring(0, 15)
					+ "...");
		} else {
			holder.txtTenmonan.setText(objCookpads.get(position)
					.getNameCookpad());
		}

		holder.txtViewer.setText(""
				+ objCookpads.get(position).getViewedCookpad());
		holder.txtLiker
				.setText("" + objCookpads.get(position).getLikeCookpad());

		holder.imgMonan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent c = new Intent(context, ChitietCongthucActivity.class);
				c.putExtra("objCookpad", objCookpads.get(position));
				v.getContext().startActivity(c);
			}
		});
		
		

		Animation anim = AnimationUtils.loadAnimation(getContext(),
				R.anim.gridview_fly_in_from_top_corner);
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
			v.startAnimation(anim);
		}
		anim.start();

		return v;
	}

	static class ViewHolder {
		ImageView imgMonan;
		TextView txtTenmonan;
		TextView txtViewer;
		TextView txtLiker;
	}

	// sort list object moi nhat
	public void sortByNewAsc() {
		Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {

			@Override
			public int compare(ObjCookpad obj1, ObjCookpad obj2) {

				return ((Integer) obj2.getViewedCookpad())
						.compareTo((Integer) obj1.getViewedCookpad());
			}
		};
		Collections.sort(objCookpads, comparator);
		notifyDataSetChanged();
	}

	// sort list xemnhieur
	public void sortByViewAsc() {
		Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {

			@Override
			public int compare(ObjCookpad obj1, ObjCookpad obj2) {

				return ((Integer) obj2.getViewedCookpad())
						.compareTo((Integer) obj1.getViewedCookpad());
			}
		};
		Collections.sort(objCookpads, comparator);
		notifyDataSetChanged();
	}

	// sap xep theo yeu thich
	public void sortByLikeAsc() {
		Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {

			@Override
			public int compare(ObjCookpad obj1, ObjCookpad obj2) {

				return ((Integer) obj2.getLikeCookpad())
						.compareTo((Integer) obj1.getLikeCookpad());
			}
		};
		Collections.sort(objCookpads, comparator);
		notifyDataSetChanged();
	}

	/*
	 * // sap xep theo danh gia public void sortByRateAsc() {
	 * Comparator<ObjCookpad> comparator = new Comparator<ObjCookpad>() {
	 * 
	 * @Override public int compare(ObjCookpad obj1, ObjCookpad obj2) {
	 * 
	 * return ((Integer) obj2.getRatingCookpad()) .compareTo((Integer)
	 * obj1.getRatingCookpad()); } }; Collections.sort(objCookpads, comparator);
	 * notifyDataSetChanged(); }
	 */

}
